//
//  NSString+Extension.m
//

#import "NSString+Extension.h"
#import "RegexKitLite.h"
#import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access

@implementation NSString (Extension)

-(CGSize)sizeWithFont:(UIFont *)font{
    return [self sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];
}
-(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size{
    return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil] context:nil].size;
}

+(NSString *)randomStringWithLength:(NSUInteger)length{
    NSUInteger NUMBER_OF_CHARS = length;
    char data[NUMBER_OF_CHARS];
    for (int x=0;x<NUMBER_OF_CHARS;data[x++] = (char)('A' + (arc4random_uniform(26))));
    return [[NSString alloc] initWithBytes:data length:NUMBER_OF_CHARS encoding:NSUTF8StringEncoding];
}

-(NSString *)trim{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (NSString *)replaceCharacter:(NSString *)oStr withString:(NSString *)nStr
{
    NSMutableString *_str = [NSMutableString stringWithString:self];
    [_str replaceOccurrencesOfString:oStr withString:nStr options:NSCaseInsensitiveSearch range:NSMakeRange(0, _str.length)];
    return _str;
}

+ (NSString *)pathDocumentsWithFileName:(NSString *)filename{
    NSString *pngDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *FileFullPath = [pngDir stringByAppendingPathComponent:filename];
    return FileFullPath;
}


+(NSString *)paramStringWithParamDic:(NSDictionary *)dic{
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                 encoding:NSUTF8StringEncoding];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@" " withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\"\"" withString:@"\" \""];
    return jsonString;
}


+(BOOL)isBlankString:(NSString *)string{
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if(![string isKindOfClass:[NSString class]]){
        string = [NSString stringWithFormat:@"%@",string];
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

//返回字符串经过md5加密后的字符
+(NSString*)stringDecodingByMD5:(NSString*)str
{
    const char * cStr = [str UTF8String];
    unsigned char result[32];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), result );
    return [NSString stringWithFormat:
            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

-(NSString*)md5DecodingString
{
    return [NSString stringDecodingByMD5:self];
}
/**
 *  校验手机号码是否合法
 *
 *  @param phoneNum 手机号码
 *
 *  @return 是否合法
 */
+(BOOL)isValidatePhone:(NSString *)phoneNum{
    NSString *regEx = @"\\b(1)[34578][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\\b";
    return [phoneNum isMatchedByRegex:regEx];
}
/**
 *  判断邮箱是否合法
 *
 *  @param email 邮箱
 *
 *  @return
 */
+(BOOL)isValidateEmail:(NSString *)email {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    return [email isMatchedByRegex:emailRegex];
}

-(BOOL)isValidatePhone{
    return [NSString isValidatePhone:self];
}

-(BOOL)isValidateEmail{
    return [NSString isValidateEmail:self];
}

- (NSString *)stringByDecodingURLFormat
{
    NSString *result = [(NSString *)self stringByReplacingOccurrencesOfString:@"+" withString:@" "];
    result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return result;
}

@end
